package com.bluetooth.demo;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.bluetooth.demo.model.BaseResult;
import com.bluetooth.demo.model.Student;
import com.bluetooth.demo.utils.HttpUtils;
import com.bluetooth.demo.utils.JsonUtils;
import com.google.gson.reflect.TypeToken;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;


/**
 * 登录界面
 */
public class LoginActivity extends FragmentActivity implements View.OnClickListener {

    private EditText et_user;
    private EditText et_password;
    private Button btn_login;
    private SharedPreferences pref;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        pref = getSharedPreferences("config", MODE_PRIVATE);

        initView();

        et_user.setText(pref.getString("username", ""));
        et_password.setText(pref.getString("password", ""));

    }

    //控件初始化
    private void initView() {
        setTitle("用户登录");
        et_user = (EditText) findViewById(R.id.et_user);
        et_password = (EditText) findViewById(R.id.et_password);
        btn_login = (Button) findViewById(R.id.btn_login);
        btn_login.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login://登录

                String name = et_user.getText().toString().trim();
                String pass = et_password.getText().toString().trim();

                pref.edit().putString("username", name).putString("password", pass).apply();

                HttpUtils.login(name, pass, new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                        Toast.makeText(LoginActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {


                        BaseResult<Student> baseResult = JsonUtils.fromJson(response, new TypeToken<BaseResult<Student>>() {
                        }.getType());

                        if (baseResult == null) {
                            Toast.makeText(LoginActivity.this, "系统错误！", Toast.LENGTH_SHORT).show();
                            return;
                        }

                        if (baseResult.isSucceed()) {
                            MyApplication.getInstance().setStudent(baseResult.getData());
                            Toast.makeText(LoginActivity.this, "登录成功！", Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(LoginActivity.this, MainActivity.class));
                            finish();
                        } else {
                            Toast.makeText(LoginActivity.this, baseResult.getMessage(), Toast.LENGTH_SHORT).show();
                        }

                    }
                });

                break;
        }
    }

}
